import * as THREE from 'three'
import type { Row } from '@/stores/datacenter'

export class RowMeshFactory {

    static createIndicatorLine(row: Row): THREE.Line | null {
        if (row.cabinets.length === 0) return null

        const totalLength = row.totalLength
        const lineGeometry = new THREE.BufferGeometry()
        const lineMaterial = new THREE.LineBasicMaterial({
            color: row.color,
            linewidth: 3
        })

        const points = [
            new THREE.Vector3(row.position.x - 500, 100, row.position.z), // 0.5m offset, 10cm height
            new THREE.Vector3(row.position.x + totalLength + 500, 100, row.position.z)
        ]
        lineGeometry.setFromPoints(points)

        const rowLine = new THREE.Line(lineGeometry, lineMaterial)
        return rowLine
    }

    static createLabel(row: Row): THREE.Mesh | null {
        if (row.cabinets.length === 0) return null

        const textGeometry = new THREE.PlaneGeometry(1000, 200) // 1m x 20cm
        const textMaterial = new THREE.MeshBasicMaterial({
            color: row.color,
            transparent: true,
            opacity: 0.8
        })
        const textMesh = new THREE.Mesh(textGeometry, textMaterial)
        textMesh.position.set(row.position.x - 1000, 200, row.position.z) // 1m offset, 20cm height
        textMesh.rotation.x = -Math.PI / 2

        return textMesh
    }

    static createIndicators(row: Row): (THREE.Line | THREE.Mesh)[] {
        const indicators: (THREE.Line | THREE.Mesh)[] = []

        const line = this.createIndicatorLine(row)
        const label = this.createLabel(row)

        if (line) indicators.push(line)
        if (label) indicators.push(label)

        return indicators
    }

    static create(row: Row): THREE.Group {
        const rowGroup = new THREE.Group()

        // 添加行指示器
        const indicators = this.createIndicators(row)
        indicators.forEach(indicator => rowGroup.add(indicator))

        return rowGroup
    }
} 